package com.zlm.linked;

/**
 * @author zlm
 * @date 2025/4/29
 * 查询链表中间节点
 */
public class SearchMid {

    public static void main(String[] args) {
        Node<Integer> head = new Node<>(1, null);
        head.next = new Node<>(2, null);
        head.next.next = new Node<>(3, null);
        head.next.next.next = new Node<>(4, null);
        head.next.next.next.next = new Node<>(5, null);
        head.next.next.next.next.next = new Node<>(6, null);
        System.out.println(searchMid(head).value);

    }

    public static <E> Node<E> searchMid(Node<E> head) {
        //慢指针
        Node<E> slow = head;
        //快指针
        Node<E> fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}
